package com.yswee.mallnavi.activity.base;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.yswee.framework.R;
import com.yswee.framework.utils.FileUtils;
import com.yswee.framework.utils.ImageUtils;
import com.yswee.framework.widget.zoom.ZoomImageLayout;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 查看大图页面
 * @author Administrator
 *
 */
public class ZoomImageActivity extends LoadingActivity {
	
	public static final String INTENT_KEY_URL = "intent_url";
	public static final String INTENT_KEY_INDEX = "intent_index";

	private ImageView imgdownload;
	private ZoomImageLayout zoomimagelayout;
	private TextView tvcurrect;
	private TextView tvtotal;
	
	private int mIndex;
	private String[] mImageUrls;

	@Override
	protected boolean isFullScreen() {
		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zoomimage);
	}
	
	@Override
	protected void onFindView() {
		super.onFindView();
		imgdownload = (ImageView) findViewById(R.id.imgdownload);
		zoomimagelayout = (ZoomImageLayout) findViewById(R.id.zoomimagelayout);
		tvcurrect = (TextView) findViewById(R.id.tvcurrect);
		tvtotal = (TextView) findViewById(R.id.tvtotal);
	}
	
	@Override
	protected void onQueryArguments(Intent intent) {
		super.onQueryArguments(intent);
		mImageUrls = intent.getStringArrayExtra(INTENT_KEY_URL);
		mIndex = intent.getIntExtra(INTENT_KEY_INDEX, 0);
	}
	
	@Override
	protected void onBindListener() {
		super.onBindListener();
		zoomimagelayout.setOnSingleTapListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		imgdownload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Bitmap bitmap = zoomimagelayout.getCurrentBitmap();
				if (bitmap == null) {
					showToastMessage(R.string.zoomimage_empty);
					return;
				}
				String folder = FileUtils.getCachePath();
				String filename = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";
				ImageUtils.saveBitma2Disk(bitmap, FileUtils.getCachePath(), filename);
				ImageUtils.saveFile2Album(folder + File.separator + filename);
				showToastMessage(getString(R.string.zoomimage_saveto, 
						folder + File.separator + filename));
			}
		});
		zoomimagelayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				tvcurrect.setText(String.valueOf(position + 1));
			}
			
			@Override
			public void onPageScrolled(int position,float positionOffset, int positionOffsetPixels) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				
			}
		});
	}
	
	@Override
	public void onApplyData() {
		super.onApplyData();
		zoomimagelayout.setDataSource(mImageUrls);
		zoomimagelayout.setSelectedIndex(mIndex);
		tvcurrect.setText(String.valueOf(mIndex + 1));
		tvtotal.setText(String.valueOf(mImageUrls.length));
	}

}
